<?php

$_SESSION['page'] = 'photo';
$pid = isset($_GET['id']) ? intval($_GET['id']) : 0;
$did = isset($_GET['did']) ? intval($_GET['did']) : 0;

if (isset($_GET['did']) && $_GET['did'] > 0) {
    $imagename = Db::getOneField('url', 'photo', 'id=' . $did);
    if (Db::delete('photo', "id=$did")) {
        unlink($imagename);
        Js::gourl(baseUrl() . '/home#photobox');
        exit;
    }
}

if (Http::isPost()) {
    $name = '';
    $url = '';
    if (Form::isValid()) {
        $curpath = $_SERVER['DOCUMENT_ROOT'] . str_replace('/admin', '/upload/', baseUrl());

        if ($_FILES["file"]["error"] > 0 && $_FILES["file"]["error"] != '4') {
            Js::alert("Có lổi: " . $_FILES["file"]["error"] . "<br />");
        } else if ($_FILES["file"]["error"] == '4') {
            Js::alert('Phải có hình đại diện');
            Js::GoUrl(baseUrl() . '/camnangAd');
        } else {
            $upload = Http::UploadImage('file', str_replace('//', '/', $curpath), '', $name);
            $url = $curpath . $name;
            if ($upload === '') {
                $eval = eval(Db::insert('photo', array_merge(array('id','type'))));
                if (mysql_affected_rows() > 0) {
                    Js::alert(fl('success_insert'));
                    Js::GoUrl(baseUrl() . '/photos');
                    exit();
                }
            } else {
                echo $upload;
            }
        }
    }
}
?>
